package com.cloudspace.jindun.manager;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;

import com.cloudspace.jindun.JindunApp;

import java.util.List;

import static android.content.Context.LOCATION_SERVICE;

/**
 * Created by zengxianhua on 17/2/5.
 */

public class MyLocationManager {
    private static Context mContext;
    private LocationManager locationManager;
    private static final int MINTIME = 5000;
    private static final int MININSTANCE = 2;
    private static MyLocationManager instance;
    private Location lastLocation = null;
    private static LocationCallBack mCallback;

    public static void init(Context c, LocationCallBack callback) {
        mContext = c;
        mCallback = callback;
    }

    private MyLocationManager() {
        // Gps 定位
        if (!isOpen(JindunApp.getInstance())){
            openGPS(JindunApp.getInstance());
        }

        getGPS();
    }

    public static MyLocationManager getInstance() {
        if (null == instance) {
            instance = new MyLocationManager();
        }
        return instance;
    }

    private void updateLocation(Location location) {
        if (location != null){
            lastLocation = location;
            mCallback.onCurrentLocation(location);
        }
    }

    private final LocationListener locationListener = new LocationListener() {

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        public void onProviderEnabled(String provider) {
            getGPS();
        }

        public void onProviderDisabled(String provider) {
        }

        public void onLocationChanged(Location location) {
            updateLocation(location);
        }
    };

    /**
     * GPS功能已经打开-->根据GPS去获取经纬度
     */
    public void getGPS() {
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
            lastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        }

        if (lastLocation == null && locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
            lastLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }

        if (lastLocation == null){
            lastLocation = getLastKnownLocation();
        }

//        if (lastLocation == null){
//            lastLocation = new Location(LocationManager.GPS_PROVIDER);
//            lastLocation.setLongitude(116.44004);
//            lastLocation.setLatitude(39.970173);
//        }
    }

    private Location getLastKnownLocation() {
        List<String> providers = locationManager.getProviders(true);
        Location bestLocation = null;
        for (String provider : providers) {
            Location l = locationManager.getLastKnownLocation(provider);
            if (l == null) {
                continue;
            }
            if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
                // Found best last known location: %s", l);
                bestLocation = l;
            }
        }
        return bestLocation;
    }

    /**
     * 判断手机GPS是否开启
     * @param
     * @return
     */
    public boolean isOpen(Context context) {
        locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
        //通过GPS卫星定位,定位级别到街
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        //通过WLAN或者移动网络确定位置
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps || network) {
            return true;
        }
        return false;
    }

    /**
     * 开启手机GPS
     */
    public void openGPS(Context context) {
        Intent GPSIntent = new Intent();
        GPSIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvide");
        GPSIntent.addCategory("android.intent.category.ALTERNATIVE");
        GPSIntent.setData(Uri.parse("custom:3"));

        try {
            //使用PendingIntent发送广播告诉手机去开启GPS功能
            PendingIntent.getBroadcast(context, 0, GPSIntent, 0).send();
        } catch (PendingIntent.CanceledException e) {
            e.printStackTrace();
        }
    }

    public Location getMyLocation() {
        return lastLocation;
    }

    private static int ENOUGH_LONG = 1000 * 60;

    public interface LocationCallBack {
        /**
         * 当前位置
         *
         * @param location
         */
        void onCurrentLocation(Location location);
    }

    public void destoryLocationManager() {
        locationManager.removeUpdates(locationListener);
    }
}
